﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LuaInterface;
using ElementumForever.Enums;
using ElementumForever.Interfaces;
using Microsoft.Xna.Framework.Input;

namespace ElementumForever.Utility
{
    class UnitHelper
    {
        public static Vector2 ToVector2(float x, float y)
        {
            return new Vector2(x, y);
        }

        public static Vector2 ToVector2(Point point)
        {
            return new Vector2(point.X, point.Y);
        }

        public static float Distance(Vector2 v1, Vector2 v2)
        {
            return Vector2.Distance(v1, v2);
        }

        static public Vector2 GetRotationVector(Vector2 vector, float theta)
        {
            Vector2 res;
            res.X = (float)(vector.X * Math.Cos(theta) - vector.Y * Math.Sin(theta));
            res.Y = (float)(vector.X * Math.Sin(theta) + vector.Y * Math.Cos(theta));
            return res;
        }

        static public float ToRadians(float degrees)
        {
            return MathHelper.ToRadians(degrees);
        }

        static public float ToDegrees(float radians)
        {
            return MathHelper.ToDegrees(radians);
        }

        public static Microsoft.Xna.Framework.Color ToColorXna(System.Drawing.Color colorForm)
        {
            Microsoft.Xna.Framework.Color colorXna = new Color(colorForm.R, colorForm.G, colorForm.B, 255);
            return colorXna;
        }

        public static System.Drawing.Color ToColorForm(Microsoft.Xna.Framework.Color colorXna)
        {
            System.Drawing.Color colorForm = System.Drawing.Color.FromArgb(colorXna.R, colorXna.G, colorXna.B);
            return colorForm;
        }

        public static System.Drawing.Color ToColorForm(byte r, byte g, byte b)
        {
            System.Drawing.Color colorForm = System.Drawing.Color.FromArgb(r, g, b);
            return colorForm;
        }

        private static bool ContainKey(LuaTable table, string key)
        {
            bool contain = false;
            foreach (string _key in table.Keys)
            {
                if (key == _key)
                    contain = true;
            }
            return contain;
        }

        public static Microsoft.Xna.Framework.Color ToColor(LuaTable colorTable)
        {
            Color color = new Color();
            color.R = Convert.ToByte(colorTable["r"]);
            color.G = Convert.ToByte(colorTable["g"]);
            color.B = Convert.ToByte(colorTable["b"]);
            if (ContainKey(colorTable, "a"))
                color.A = Convert.ToByte(colorTable["a"]);
            return color;
        }

        public static EObjectClass ToEObjectClass(string value)
        {
            EObjectClass eo = (EObjectClass)Enum.Parse(typeof(EObjectClass), value);
            return eo;
        }

        ///// <summary>
        ///// Rotate vector by angle in radians,center:(0,0)
        ///// </summary>
        ///// <param name="source">vector to rotate
        ///// </param>
        ///// <param name="angle">angle in radians
        ///// </param>
        ///// <returns></returns>
        //public static Vector2 Rotation(Vector2 source, float angle)
        //{
        //    float radians = ToRadians(angle);
        //    Vector2 rotated = new Vector2(source.X, source.Y);
        //    rotated.X = (float)(source.X * Math.Cos(radians) - source.Y * Math.Sin(radians));
        //    rotated.Y = (float)(source.Y * Math.Cos(radians) + source.X * Math.Sin(radians));
        //    return rotated;
        //}

        ///// <summary>
        ///// Rotate vector by angle in radians,center
        ///// </summary>
        ///// <param name="source">vector to rotate
        ///// </param>
        ///// <param name="center">center
        ///// </param>
        ///// <param name="angle">angle in radians
        ///// </param>
        ///// <returns></returns>
        //public static Vector2 Rotation(Vector2 source, Vector2 center, float angle)
        //{
        //    Vector2 rotated = new Vector2(source.X - center.X, source.Y - center.Y);
        //    rotated = Rotation(rotated, angle) + center;
        //    return rotated;
        //}

        //public static MouseState GetMouseStateInScene(BaseScene scene)
        //{
        //    MouseState m = Mouse.GetState();
        //    Vector2 sv = CoordinateHelper.FromScreenToScene(new Vector2(m.X, m.Y),scene);
        //    MouseState _state = new MouseState((int)sv.X, (int)sv.Y, m.ScrollWheelValue, m.LeftButton, m.MiddleButton, m.RightButton, m.XButton1, m.XButton2);
        //    return _state;
        //}



    }
}
